package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.Exceptions.CheckItemException;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @outhor
 * @date 2020/2/10 19:50
 */

@RequestMapping("permission")
@RestController
public class permissionController {

    @Reference
    private PermissionService permissionService;

    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult page =  permissionService.findPage(queryPageBean);
            return new Result(true,"查询权限数据成功",page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询权限数据失败");
        }
    }

    @RequestMapping("add")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
            return new Result(true,"添加权限数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加权限数据失败");
        }
    }

    @RequestMapping("findById")
    public Result findById(Integer id){
        Permission permission = permissionService.findById(id);
        return new Result(true,"",permission);
    }

    @RequestMapping("updata")
    public Result updata(@RequestBody Permission permission){
        try {
            permissionService.updata(permission);
            return new Result(true,"编辑权限数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑权限数据失败");
        }
    }

    @RequestMapping("delete")
    public Result delete(Integer id){
        try {
            permissionService.delete(id);
            return new Result(true,"删除权限数据成功");
        } catch (CheckItemException e) {
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除权限数据失败");
        }
    }

}
